package org.wlldTest.task;


import org.dromara.easyai.matrixTools.Matrix;
import org.dromara.easyai.matrixTools.MatrixOperation;

public class LineRegion {// w1*x+w2*y+b*1=index
    private float w1;//x系数
    private float w2;//y系数
    private float b;//平移量
    private Matrix XY;//坐标
    private Matrix E;//序列
    private int index;//下标
    private MatrixOperation matrixOperation = new MatrixOperation();

    public LineRegion(int size) {// XY * (w1,w2,b)= E
        XY = new Matrix(size, 3);
        E = new Matrix(size, 1);
        index = 0;
    }

    public float getIndex(float x, float y) {
        return x * w1 + y * w2 + b;
    }

    public void insertPosition(float x, float y, float e) throws Exception {
        XY.setNub(index, 0, x);
        XY.setNub(index, 1, y);
        XY.setNub(index, 2, 1);
        E.setNub(index, 0, e);
        index++;
    }

    public void start() throws Exception {
        Matrix matrix = matrixOperation.getLinearRegression(XY, E);
        if (matrix.getX() == 1 && matrix.getY() == 1) {//矩阵奇异
            throw new Exception("矩阵奇异!");
        } else {
            w1 = matrix.getNumber(0, 0);
            w2 = matrix.getNumber(1, 0);
            b = matrix.getNumber(2, 0);
        }
    }
}
